package com.wy.dzsw.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mysql.jdbc.StringUtils;
import com.wy.dzsw.pojo.Shop;
import com.wy.dzsw.pojo.Ttype;
import com.wy.dzsw.service.ShopService;
import com.wy.dzsw.service.TtypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @描述
 * @创建人 wangyue
 * @创建时间2020/12/816:12
 */
@Api(tags = "商品管理")
@RestController
@RequestMapping("shop")
@RefreshScope
public class ShopController {

    @Resource
    private ShopService shopService;

    @Resource
    private TtypeService ttypeService;

    @Value("${foo}")
    private String foo;

    @Value("${server.port}")
    private String port;


    //@RequestMapping(value="getAll",method = RequestMethod.GET)
    @GetMapping("getAll")
    @ApiOperation("查询所有商品")
    public List<Shop> getAll(){
        return shopService.getAll();
    }


    @GetMapping("getHj")
    public Map<String,Object> getHj(){
        HashMap<String, Object> map = new HashMap<>();
        map.put("port",port);
        map.put("foo",foo);
        return map;
    }

    /**
     * 查询所有类型
     * @return
     */
    //@ApiOperation(value = "查询所有类型")
    @GetMapping("getTypes")
    public List<Ttype> getTypes(){
        return ttypeService.list();
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("getById/{id}")
    public Shop getById(@PathVariable Integer id){
        return shopService.getById(id);
    }

    /**
     * 根据id查询
     * @return
     */
    @PostMapping("delete")
    public void delete(@RequestBody List<Integer> ids){
         shopService.removeByIds(ids);
    }
    /**
     * 根据id查询
     * @return
     */
    @PostMapping("save")
    public void save(@RequestBody Shop shop){
        shopService.saveOrUpdate(shop);
    }

    /**
     * 根据id查询
     * @return
     */
    @PostMapping("pageList")
    public Map<String,Object> pageList(@RequestBody(required =  false) Shop shop,
                                       @RequestParam(defaultValue = "3") int pageSize,
                                       @RequestParam(defaultValue = "1")int pageNum){
        Page<Shop> shopPage = new Page<>(pageNum, pageSize);
        QueryWrapper queryWrapper = new QueryWrapper();
        if(shop != null){
            if(!StringUtils.isNullOrEmpty(shop.getName())){
                queryWrapper.like("name",shop.getName());
            }
        }
        //IPage<Shop> page = shopService.page(shopPage, queryWrapper);
        IPage<Shop> page =shopService.listpage(shopPage, queryWrapper);
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",page.getTotal());
        map.put("list",page.getRecords());
        return map;
    }

    /**
     * 根据id查询
     * @return
     */
    @PostMapping("pageList2")
    public List<Shop> pageList2(@RequestBody(required =  false) Shop shop,
                                       @RequestParam(defaultValue = "3") int pageSize,
                                       @RequestParam(defaultValue = "1")int pageNum){
        QueryWrapper queryWrapper = new QueryWrapper();
        if(shop != null){
            if(!StringUtils.isNullOrEmpty(shop.getName())){
                queryWrapper.like("name",shop.getName());
            }
        }
        return shopService.pagelist(queryWrapper);
    }

}
